/** 创建组件对象 * */
var benchmarkForm = new Form();
var benchmarkTree = new Tree();
var benchmarkRightTree = new Tree();
var benchmarkTable = new Table();
var benchmarkPage = new Page();
var benchmarkDialog = new Dialog();
var benchmarkAjax = new Ajax();
var iframeIndex;
var keyName = "indicator";
var objName = "benchmarkObj";

var unitObj = [];
var packageObj = [];
var benchmarkObj =[];

/** 页面加载完执行 * */
$(function() {
	initScript();
	initBenchmarkTree();
	initButton();
	initElementsControl();
	if(packageObj!=null){
		initBenchmarkRightTree();
	}
	if(benchmarkObj!=[]){
		initTable();
	}
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/benchmark/locale/Benchmark_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/benchmark/properties/BenchmarkUnitProperties_Dialog.js"); // 2.加载配置js(非必配,配置也可写在本js中)
	loadScript(appPath + "/project-yddemo/common/Common.js");
}

function initBenchmarkTree() {
	if(window.parent[objName]!=undefined){
		benchmarkObj = window.parent[objName];
	}
	if(window.parent.unitObj!=undefined){
		unitObj = window.parent.unitObj;
	}
	if(window.parent.packageObj!=undefined){
		packageObj = window.parent.packageObj;
	}
	var unitIds = [];
	var data = {};
	for(var i = 0;i<unitObj.length;i++){
		unitIds.push(unitObj[i].id);
	}
	if(unitIds.length!=0){
		data.unitIds = unitIds.join(",");
	}else{
		data.unitIds = "";
	}
	benchmarkTree.initTree({
		divId : "benchmarkTreeDiv", // 归属div*
		ajaxType : "ajax",
		root : {
			text : benchmarkLocale.field.electric,
			leaf : false,
			expanded : true
		},
		dataAjax : { // 数据源ajax*
			url : requestPath + "/yddemo/benchmarks/Unitdialog", // 请求url*
			async : false, // 是否异步*
			type : "GET", // 请求类型*
			data : data
		},
		align : { // 元素顺序
			left : "expand",
			right : "check",
			center : "text"
		},
		checkIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_check.png", // 选中图标
		partcheckIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_partcheck.png", // 部分选图标
		uncheckIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_uncheck.png", // 未选图标
		expandIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_main_expand.png", // 展开图标
		closeIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_main_shrink.png", // 闭合图标
		textClickFun : function(nodeId) { // 文本单击事件*
			benchmarkTree.expandDefaultFun(nodeId);
		},
		expandFun : function(nodeId) { // 展开单击事件*
			benchmarkTree.expandDefaultFun(nodeId);
		},
		checkFun : function(nodeId){
			benchmarkTree.checkDefaultFun(nodeId);
			unitObj = benchmarkTree.getCheckedNodes();
			initBenchmarkRightTree();
		}
	});
	if (!$.isEmptyObject(sessionStorage.keyName)) {
		keyName = sessionStorage.keyName;
		sessionStorage.removeItem("keyName");
	}
	if (!$.isEmptyObject(sessionStorage.objName)) {
		objName = sessionStorage.objName;
		sessionStorage.removeItem("objName");
	}
}

function initBenchmarkRightTree() {
	var unitIds = [];
	var packageIds = [];
	var data = {};
	for(var i = 0;i<unitObj.length;i++){
		unitIds.push(unitObj[i].id);
	}
	if(unitIds.length!=0){
		data.unitIds = unitIds.join(",");
	}else{
		data.unitIds = "";
		packageObj = [];
		benchmarkObj = [];
		initTable();
	}
	for(var i =0;i<packageObj.length;i++){
		packageIds.push(packageObj[i].id);
	}
	if(packageIds.length!=0){
		data.packageIds = packageIds.join(",");
	}else{
		data.packageIds = "";
	}
	benchmarkRightTree.initTree({
		divId : "benchmarkRightTreeDiv", // 归属div*
		ajaxType : "ajax",
		root : {
			text : benchmarkLocale.field.indicatorManager,
			leaf : false,
			expanded : true
		},
		dataAjax : { // 数据源ajax*
			url : requestPath + "/yddemo/benchmarks/UnitPackage", // 请求url*
			async : false, // 是否异步*
			type : "GET", // 请求类型*
			data : data
		},
		align : { // 元素顺序
			left : "expand",
			right : "check",
			center : "text"
		},
		checkIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_check.png", // 选中图标
		partcheckIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_partcheck.png", // 部分选图标
		uncheckIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_uncheck.png", // 未选图标
		expandIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_main_expand.png", // 展开图标
		closeIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_main_shrink.png", // 闭合图标
		textClickFun : function(nodeId) { // 文本单击事件*
			benchmarkTree.expandDefaultFun(nodeId);
		},
		expandFun : function(nodeId) { // 展开单击事件*
			benchmarkRightTree.expandDefaultFun(nodeId);
		},
		checkFun : function(nodeId){
			benchmarkRightTree.checkDefaultFun(nodeId);
			packageObj = benchmarkRightTree.getCheckedNodes();
			benchmarkObj = [];
			initTable();
		}
	});
}

function initTable(){
	var packageIds = [];
	var indicatorIds = [];
	var data = {};
	for(var i =0;i<packageObj.length;i++){
		packageIds.push(packageObj[i].id);
	}
	if(packageIds.length!=0){
		data.packageIds = packageIds.join(",");
	}else{
		data.packageIds = "";
	}
	benchmarkTable.initTable({
		divId : "benchmarkTableDiv", // 1.归属div*
		dataAjax : { // 数据源ajax*
			url : requestPath + "/yddemo/benchmarks/UnitIndicator", // 请求url*
			async : false, // 是否异步*
			type : "GET", // 请求类型*
			data : data
		},
		columns : BenchmarkUnitProperties_Dialog.getViewColumns()// 3.表格列配置
	});
	benchmarkTable.reloadData();
	for(var i =0;i<benchmarkObj.length;i++){
		indicatorIds.push(benchmarkObj[i].id);
	}
	benchmarkTable.setSelectRows(indicatorIds, false);
}


function initButton() {
	$(".formButtonDiv")
			.append(
					"<div style='width:50%;'>"
							+ "<span class='formButton' style='float:right; margin-right:10%;' onclick='closeDialog()'>"
							+ benchmarkLocale.text.close
							+ "</span>"
							+ "</div>"
							+ "<div class='action' style='width:50%;'>"
							+ "<span class='formButton' style='float:left; margin-right:10%;' onclick='saveData()'>"
							+ benchmarkLocale.text.okey + "</span>" + "</div>");
}

/** 保存数据 * */
function saveData() {
	benchmarkObj = benchmarkTable.getSelectDatas();
	setBenchmarkInput();
	if(benchmarkObj.length>0){
		window.parent[objName] = benchmarkObj;
		window.parent.unitObj = unitObj;
		window.parent.packageObj = packageObj;
	}else{
		window.parent[objName] = [];
		window.parent.unitObj = [];
		window.parent.packageObj = [];
	}
	// 关闭弹出框
	var div = window.frameElement.parentElement;
	var shadeDiv = $(div).parent();
	$(div).remove();
	if (shadeDiv.children().length == 0) {
		shadeDiv.remove();
	}
}

function closeDialog() {
	// 关闭弹出框
	var div = window.frameElement.parentElement;
	var shadeDiv = $(div).parent();
	$(div).remove();
	if (shadeDiv.children().length == 0) {
		shadeDiv.remove();
	}
}

function setBenchmarkInput() {
	if (benchmarkObj != undefined) {
		var valContext="";
		for(var i = 0;i<benchmarkObj.length;i++){
			valContext += benchmarkObj[i].indicator.name+",";
		}
		window.parent.$("[key$='" + keyName + "']").val(valContext);
	}
}